//uploadFile.js
const multer=require("multer");
const path=require("path");

module.exports=(req,res,next)=>{
    console.log('req.body',req.body);
    let fullPath=path.resolve("./views/imgStoreHouse/avatar");
    let filename="";
    let storage=multer.diskStorage({
    	//设置文件存储路径
        destination:(req,file,cb)=>{
            cb(null,fullPath);
        },
        //设置文件存储名称
        filename:(req,file,cb)=>{
            console.log('file: ', file);
            let extname=path.extname(file.originalname);
            console.log('extname: ', extname);
            // 图片重命名
            // filename=file.fieldname+"-"+Date.now()+extname;
            filename=file.originalname;

            //得到服务器中图片的资源路径
        //    console.log('req.body: ', req.body);
            let src='';
            src="/imgStoreHouse/avatar/"+filename;
            // src="/imgStoreHouse/product/"+filename;
           console.log('src: ', src);
           cb(null,filename);
        //    console.log('req.body.desc: ', req.body.desc);
        }
    });
	//上传单张图片
    let upload=multer({storage}).single("photo");
    upload(req,res,(err)=>{
       if (err instanceof multer.MulterError) {
            res.send("multererr:"+err);
        }else if(err){
            res.send("err:"+err);
        }else{
            //上传成功后，将图片写在req.body.photo中，继续住下执行
            req.body.photo=filename;
            next();
        }
    });
}
